from rest_framework import serializers

from course.models import Category, Book


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'


class BookSerializer(serializers.ModelSerializer):
    category_info = CategorySerializer(source='category', read_only=True)

    class Meta:
        model = Book
        fields = [
            'id',
            'name',
            'writer',
            'ISBN',
            'status',
            'category',
            'description',
            'create_time',
            'update_time',
            'category_info'
        ]


    def get_category_info(self, obj):
        return obj.category.name
